//
//  AddFriendView.m
//  BaseAPP
//
//  Created by Evisu on 16/12/20.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "AddFriendView.h"
#import "AddFriendCell.h"

@interface AddFriendView ()<UITableViewDataSource, UITableViewDelegate>

//搜索框
@property (nonatomic, strong)UIView *vSearchView;
@property (nonatomic, strong)UIImageView *ivSearch;
@property (nonatomic, strong)UILabel *labWechatPhone;

//我的微信号
@property (nonatomic, strong)UILabel *labMyWechat;

//二维码
@property (nonatomic, strong)UIImageView *ivQRCode;

//雷达加朋友、面对面建群...
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *datas;

@end

@implementation AddFriendView

- (instancetype)init {

    if (self = [super init]) {
        
        self.datas = @[@[@"雷达加朋友", @"添加身边的朋友", @"add_friend_icon_reda"], @[@"面对面建群",@"与身边的朋友进入同一个群聊", @"add_friend_icon_addgroup"], @[@"扫一扫",@"扫描二维码名片", @"add_friend_icon_scanqr"], @[@"手机联系人",@"添加通讯录中的朋友", @"add_friend_icon_contacts"], @[@"公众号",@"获取更多资讯和服务", @"add_friend_icon_offical"]];
        
        [self initViews];
        [self layoutViews];
        
        [self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    }
    
    return self;
}

- (void)initViews {

    self.backgroundColor = HEXColor(@"f0eff5");
    
    self.vSearchView = [[UIView alloc] init];
    self.vSearchView.backgroundColor = [UIColor whiteColor];
    self.vSearchView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedSearchView)];
    [self.vSearchView addGestureRecognizer:tap];
    
    self.ivSearch = [[UIImageView alloc] init];
    self.ivSearch.image = [UIImage imageNamed:@"add_friend_searchicon"];

    self.labWechatPhone = [[UILabel alloc] init];
    self.labWechatPhone.text = @"微信号/手机号";
    self.labWechatPhone.textColor = HEXColor(@"cdccd1");
    
    self.labMyWechat = [[UILabel alloc] init];
    self.labMyWechat.text = @"我的微信号：_493879263";
    self.labMyWechat.font = [UIFont systemFontOfSize:14.0];
    self.labMyWechat.textColor = HEXColor(@"171717");
    
    self.ivQRCode = [[UIImageView alloc] init];
    self.ivQRCode.image = [UIImage imageNamed:@"add_friend_myQR"];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self addSubview:self.vSearchView];
    [self.vSearchView addSubview:self.ivSearch];
    [self.vSearchView addSubview:self.labWechatPhone];
    [self addSubview:self.labMyWechat];
    [self addSubview:self.ivQRCode];
    [self addSubview:self.tableView];
}


- (void)layoutViews {

    self.vSearchView.sd_layout
    .topSpaceToView(self, 20)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(46);
    
    self.ivSearch.sd_layout
    .centerYEqualToView(self.vSearchView)
    .leftSpaceToView(self.vSearchView, 16)
    .widthIs(36)
    .heightIs(30);
    
    self.labWechatPhone.sd_layout
    .centerYEqualToView(self.vSearchView)
    .leftSpaceToView(self.ivSearch, 18)
    .heightIs(14)
    .widthIs(150);
    
    self.labMyWechat.sd_layout
    .topSpaceToView(self.vSearchView, 10)
    .leftSpaceToView(self, 50)
    .heightIs(14)
    .widthIs([UIScreen mainScreen].bounds.size.width-100);
    
    self.ivQRCode.sd_layout
    .topSpaceToView(self.vSearchView, 8)
    .rightSpaceToView(self, 70)
    .heightIs(18)
    .widthIs(18);
    
    self.tableView.sd_layout
    .topSpaceToView(self.vSearchView, 60)
    .leftEqualToView(self)
    .rightEqualToView(self);

}

#pragma mark - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reusedId = @"addFriend";
    
    AddFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId];
    
    if (!cell) {
        
        cell = [[AddFriendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
    }
    
    cell.datas = self.datas[indexPath.row];
    
    return cell;
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {

    CGSize size = [[change valueForKey:NSKeyValueChangeNewKey] CGSizeValue];
    self.tableView.height_sd = size.height;
}

/**
 *  点击搜索view
 */
- (void)didClickedSearchView {

    if (self.searchBlock) {
        
        self.searchBlock();
    }
}

- (void)dealloc {

    [self.tableView removeObserver:self forKeyPath:@"contentSize" context:nil];
}


@end
